/**
 * @author attilax 老哇的爪子
\t@since  Aug 23, 2014 8:26:13 PM$
 */
package com.attilax.clr;
import com.attilax.Closure;
import com.attilax.core;
import com.attilax.io.filex;
import com.attilax.secury.MD5;
import com.attilax.secury.MD5_BigFile;
import com.attilax.util.dirx;

import static  com.attilax.core.*;

import java.util.*;
import java.net.*;
import java.io.*;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
/**
 * @author  attilax 老哇的爪子
 *@since  Aug 23, 2014 8:26:13 PM$
 */
public class garLib {

	Set<String> set=new HashSet<String>();
	Set<Long> fileSize_set=new HashSet<Long>();
	/**
	@author attilax 老哇的爪子
	\t@since  Aug 23, 2014 8:26:51 PM$
	
	 * @param dir
	 */
	public garLib(String dir) {
		//  attilax 老哇的爪子 8:26:51 PM   Aug 23, 2014   
		dirx.trave(dir, new Closure<String, String>() {

			@Override
			public String execute(String f) throws Exception {
				// attilax 老哇的爪子  8:28:10 PM   Aug 23, 2014 
				
				{ byte[] ba=filex.readImageData(f);
				String md5=MD5.getMD5_RE(ba);
				set.add(md5);
				return null;
				 } 
				
				
			}
		});
	}
	
	public garLib() {
		// TODO Auto-generated constructor stub
	}

	/**
	@author attilax 老哇的爪子
	\t@since  Aug 23, 2014 8:26:13 PM$
	
	 * @param args
	 * @throws ParseException 
	 * -garb_files_md5_lib E:\ati\garbalFileMd5 -dir e:\ati\doc -gene_batfile e:\ati\del_file_by_md5.bat
	 */
	public static void main(String[] args) throws ParseException {
		// attilax 老哇的爪子  8:26:13 PM   Aug 23, 2014 

		{	
			 Options opts = new Options(); 
			  opts.addOption("dir", true, "help:mis param dir ");
	         opts.addOption("garb_files_md5_lib", true, "help:mis param garb_files_md5_lib ");
			 opts.addOption("gene_batfile", true,"help:mis param gene_batfile  "); 
		 
			 CommandLineParser parser = new PosixParser(); 
			 CommandLine cmd = parser.parse(opts, args); 
			//isGarbFile();
		new garLib().	geneDelFileBatByGarbMd5(cmd.getOptionValue("dir"),cmd.getOptionValue("garb_files_md5_lib") ,cmd.getOptionValue("gene_batfile"));
		}

	}
	//  attilax 老哇的爪子 8:26:13 PM   Aug 23, 2014   

	@SuppressWarnings("all")
	private void geneDelFileBatByGarbMd5(String dir, String garb_files_md5_lib,
			String gene_batfile) {

		// garLib gl=new garLib();
		geneGarbLib_by_filesMD5(garb_files_md5_lib);
		  final filex fx;
		try {
			  fx=new filex(gene_batfile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		dirx.trave_files_noIncludeDir(dir, new Closure() {

			@Override
			public Object execute(Object arg0) throws Exception {
				String p = (String) arg0;
				
				File f = new File(p);
			//	if(f.getName().contains("ppt"))
				if (garLib.this.fileSize_set.contains(f.length())) {
					String md5 = MD5_BigFile.getMD5(f);
					if (garLib.this.set.contains(md5))
					{
						 fx.appendLine_flush_safe(" del \"" + p+"\"");
						System.out.println(" del \"" + p+"\"");
					}
				}else
				{
					String md5 = MD5_BigFile.getMD5(f);
					if (garLib.this.set.contains(md5))
					{ fx.appendLine_flush_safe(" del \"" + p+"\"");
						System.out.println(" del \"" + p+"\"");
					}
				}

				// System.out.println("move \""+dir +"\" e:\\0");

				// System.out.println(i++);
				return null;
			}
		});
		
		try {
			fx.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@SuppressWarnings("all")
	private void geneGarbLib_by_filesMD5(String garb_files_md5_lib) {
		
		dirx.trave_files_noIncludeDir(garb_files_md5_lib, new Closure () {

			@Override
			public Object execute(Object arg0) throws Exception {
				 String p=(String) arg0;
				 
			
				  File f = new File(p);
				List<String> set=filex.read2list(p);
				for (String line : set) {
					String[] a=line.split(",");
					try {
						garLib.this.set.add(a[0]);
					} catch (Exception e) {
						// TODO: handle exception
					}
				
					try {
						garLib.this.fileSize_set.add(Long.parseLong(a[1]));
					} catch (Exception e) {
						 
					}
				
				}
			
				return null;
			}
		});
		
	}

	private static void isGarbFile() {
		String f="C:\\gar";
		
		String s="C:\\gar\\2.jpg";
		garLib gl=new garLib(f);
		System.out.println(gl.isGarfile(s));
		System.out.println(garLib.$().isGarfile(s));
	}

	/**
	@author attilax 老哇的爪子
	\t@since  Aug 23, 2014 8:34:35 PM$
	
	 * @return
	 */
	public static garLib $() {
		// attilax 老哇的爪子  8:34:35 PM   Aug 23, 2014 
		
		{ 
		return new garLib("C:\\gar");
		 } 
		
		
	}

	/**
	@author attilax 老哇的爪子
	\t@since  Aug 23, 2014 8:30:12 PM$
	
	 * @param s
	 * @return
	 */
	public boolean isGarfile(String f) {
		// attilax 老哇的爪子  8:30:12 PM   Aug 23, 2014 
		
		{ byte[] ba=filex.readImageData(f);
		String md5=MD5.getMD5_RE(ba);
		
		return set.contains(md5);
		 } 
		
		
	}
	
	
 
	public boolean isGarfile(byte[] ba) {
		// attilax 老哇的爪子  8:30:12 PM   Aug 23, 2014 
		
		{  
		String md5=MD5.getMD5_RE(ba);
		
		return set.contains(md5);
		 } 
		
		
	}
}

//  attilax 老哇的爪子